//
//  MKMyViewOrdersCell.swift
//  MOKOAnnounce
//
//  Created by 曹相召 on 2017/7/10.
//  Copyright © 2017年 cc.moko. All rights reserved.
//

import UIKit

enum MKMyViewOrdersCellType {
    case Business
    case Actor
    case Brokerage
}
class MKMyViewOrdersCell: MKBaseTableViewCell {
    var line:UIView!
    var arrow:UIImageView!
    var orderOne:MKMyViewOrdersButton!
    var orderTwo:MKMyViewOrdersButton!
    var orderThree:MKMyViewOrdersButton!
    var orderFour:MKMyViewOrdersButton!
    var orderFive:MKMyViewOrdersButton!
    var businessArr:Array<Array<String>>?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.makeUI()
    }
    func makeUI() -> Void {
        self.line = UIView.init()
        self.line.backgroundColor = UIColorFromRGB(rgbValue: 0xe5e5e5, A: 1)
        self.contentView.addSubview(self.line)
        
        self.arrow = UIImageView.init(image: UIImage.init(named: "newMe_enterArrow"))
        self.contentView.addSubview(self.arrow)
        
        self.orderOne = MKMyViewOrdersButton()
        self.orderOne.tag = 1
        self.orderOne .addTarget(self, action: #selector(orderClick(btn:)), for: .touchUpOutside)
        self.contentView.addSubview(self.orderOne)
        
        self.orderTwo = MKMyViewOrdersButton()
        self.orderTwo.tag = 2
        self.orderTwo .addTarget(self, action: #selector(orderClick(btn:)), for: .touchUpOutside)
        self.contentView.addSubview(self.orderTwo)
        
        self.orderThree = MKMyViewOrdersButton()
        self.orderThree.tag = 3
        self.orderThree .addTarget(self, action: #selector(orderClick(btn:)), for: .touchUpOutside)
        self.contentView.addSubview(self.orderThree)
        
        self.orderFour = MKMyViewOrdersButton()
        self.orderFour.tag = 4
        self.orderFour .addTarget(self, action: #selector(orderClick(btn:)), for: .touchUpOutside)
        self.contentView.addSubview(self.orderFour)
        
        self.orderFive = MKMyViewOrdersButton()
        self.orderFive.tag = 5
        self.orderFive .addTarget(self, action: #selector(orderClick(btn:)), for: .touchUpOutside)
        self.contentView.addSubview(self.orderFive)
        
        self.businessArr = [["newMeIcon_daiPay","待付款"],["newMeIcon_daiWanGong","待完工"],["newMeIcon_daiPingJia","待评价"],["newMeIcon_tuiKuanZhong","退款中"],["newMeIcon_myOrders","我的订单"]]
      }
    func config(badges:Array<Int>,cellType:MKMyViewOrdersCellType) -> Void {
        switch cellType {
        case .Business:
            self.orderOne.config(icon: self.businessArr![0][0] , tip: self.businessArr![0][1], badge: badges[0])
            self.orderTwo.config(icon: self.businessArr![1][0] , tip: self.businessArr![1][1], badge: badges[1])
            self.orderThree.config(icon: self.businessArr![2][0] , tip: self.businessArr![2][1], badge: badges[2])
            self.orderFour.config(icon: self.businessArr![3][0] , tip: self.businessArr![3][1], badge: badges[3])
            self.orderFive.config(icon: self.businessArr![4][0] , tip: self.businessArr![4][1], badge: 0)

            break
        case .Actor:
            break
        case .Brokerage:
            break
        }
        
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func orderClick(btn:MKMyViewOrdersButton) -> Void {
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = realValue6(value: 280 - 14) * 0.25
        
        self.orderOne.frame = CGRect.init(x: realValue6(value: 7), y: 0, width: width, height: self.contentView.height)
        self.orderTwo.frame = CGRect.init(x: self.orderOne.right, y: 0, width: width, height: self.contentView.height)
        self.orderThree.frame = CGRect.init(x: self.orderTwo.right, y: 0, width: width, height: self.contentView.height)
        self.orderFour.frame = CGRect.init(x: self.orderThree.right, y: 0, width: width, height: self.contentView.height)
        
        self.line.frame = CGRect.init(x: realValue6(value: 280), y: (self.contentView.height - 69) * 0.5, width: 0.5, height: 69)
        
        let orderFiveWidth:CGFloat = realValue6(value: 95 - 4 - 10) - 9
        
        self.orderFive.frame = CGRect.init(x: self.line.right + realValue6(value: 4), y: 0, width: orderFiveWidth, height: self.contentView.height)
            
        self.arrow.frame = CGRect.init(x: self.contentView.right - realValue6(value: 10) - 8.5, y: (self.contentView.height - 16) * 0.5, width: 8.5, height: 16)
        }
}


class MKMyViewOrdersButton: UIButton {
    var icon:UIImageView!
    var tip:UILabel!
    var badge:UILabel!
    
    override init(frame: CGRect) {
      super.init(frame: frame)
    
        self.icon = UIImageView.init()
        self.addSubview(self.icon)
        
        self.tip = UILabel.label(fontSize: 12, textColor: UIColorFromRGB(rgbValue: 0x333333, A: 1))
        self.tip.textAlignment = .center
        self.addSubview(self.tip)
        
        self.badge = UILabel.label(fontSize: 10, textColor: UIColor.white)
        self.badge.backgroundColor = UIColorFromRGB(rgbValue: 0xff316b, A: 1)
        self.badge.textAlignment = .center
        self.addSubview(self.badge)
    }
    func config(icon:String,tip:String,badge:Int) -> Void {
        self.icon.image = UIImage.init(named: icon)
        self.tip.text = tip
        self.badge.isHidden = badge <= 0
        self.badge.text = String.init(stringInterpolationSegment: badge)
        self.setNeedsLayout()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.icon.frame = CGRect.init(x: (self.width - 42) * 0.5, y: 10, width: 42, height: 42)
        
        self.tip.sizeToFit()
        self.tip.frame = CGRect.init(x: 0, y: self.height - 10 - self.tip.height, width: self.width, height: self.tip.height)
        
        self.badge.frame = CGRect.init(x: self.icon.right - 15, y: self.icon.top, width: 15, height: 15)
        self.badge.layer.cornerRadius = 7.5
        self.badge.clipsToBounds = true
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
